<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $ID
 * @property string $username
 * @property string $password
 * @property integer $active
 * @property integer $reset
 * @property integer $createddate
 * @property integer $modifieddate
 * @property integer $deleted
 * @property integer $status
 */
class User extends CActiveRecord
{
	public $repass;
	public $oldpass;
	public $captcha;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username', 'required','on'=>'register'),
			array('username','validateUsername','on'=>'register'),
			array('password, repass', 'required'),
			array('password', 'length', 'min'=>5),
			array('repass','compare','compareAttribute'=>'password'),
			
			array('active, reset, createddate, modifieddate, deleted, status', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>255),
			
			array('captcha','required','on'=>'reset'),
		  array('captcha','captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'reset'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, username, password, active, reset, createddate, modifieddate, deleted, status', 'safe', 'on'=>'search'),
		);
	}
  /**
   * Check username if duplicate
   */
	public function validateUsername($att, $param) {
	  $this->username = strtolower(trim($this->username));
	  $user = User::model()->find(array('condition'=>'LCASE(`username`)=:username','params'=>array(':username'=>$this->username)));
	  if(!empty($user)) {
	    $this->addError('username', 'Tên đăng nhập này đã tồn tại');
	    return false;
	  }
	  return true;
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'ID',
			'username' => 'Tên đăng nhập',
			'password' => 'Mật khẩu',
		  'repass'=> 'Xác nhận mật khẩu',
			'active' => 'Kích hoạt',
			'reset' => 'Reset',
			'createddate' => 'Ngày tạo',
			'modifieddate' => 'Ngày sửa',
			'deleted' => 'Deleted',
			'status' => 'Trạng thái',
			'fullname'=>'Họ tên',
			'captcha'=> 'Mã xác nhận'
		);
	}
	
	protected function beforeSave() {
		if($this->isNewRecord) {
			$this->createddate = time();
		}
			$this->modifieddate = time();
		return parent::beforeSave();
	}
	
	public function getYesNo($type) {
		$string = "";
		if($type==1) {
			$string = "Có";
		} else {
			$string = "Không";
		}
		return $string;
	}
	
	public function getUserStatus($type) {
		$string = "";
		if($type==1) {
			$string = "Online";
		} else if($type==2) {
			$string = "Busy";
		} else{
			$string ="Offline";
		}
		return $string;
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->together = true;
		$criteria->select = 't.*,up.fullname as fullname';
		$criteria->join = 'JOIN tbl_user_profile as up ON t.id=up.userID';
		$criteria->compare('ID',$this->ID);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('active',$this->active);
		$criteria->compare('reset',$this->reset);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
        'attributes'=>array(
            'fullname'=>array(
                'asc'=>'fullname',
                'desc'=>'fullname DESC',
            ),
            '*',
        ),
   		),
		));
	}
}